﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataCentral.Dataset;
using DataCentral.Dataset.DataCentralDatasetTableAdapters;

namespace DataCentral.UserControls
{
    public partial class ConvertSaleOrderControl : DataCentral.CommonCenterControl
    {
        DebtAdjust_DateTableAdapter daDebtAdjust = new DebtAdjust_DateTableAdapter();
        public ConvertSaleOrderControl()
        {
            InitializeComponent();
        }

        private decimal CalculateLineFreeAmt(string orderNbr)
        {
            decimal result = 0;
            DataRow[] lstOrderDetail = dsCentral.DspOrdDet.Select(string.Format("OrderNbr = '{0}'",orderNbr));
            for (int i=0; i<lstOrderDetail.Length;i++)
            {
                DataCentralDataset.DspOrdDetRow drOrderDetail = (DataCentralDataset.DspOrdDetRow)lstOrderDetail[i];
                if (drOrderDetail.FreeItem > 0)
                {
                    DataCentralDataset.InventoryRow drInventory = dsCentral.Inventory.FindByInventoryID(Convert.ToDecimal(drOrderDetail.InvtId));
                    if (drInventory != null)
                        result += (decimal)(drOrderDetail.FreeItem * drInventory.StkBasePrc);
                }
            }
            return result;
        }

        private void AssignValueToARDoc(DataCentralDataset.SlsOrdDspRow drSaleDsp, DataCentralDataset.ARSaleDocRow drARDoc)
        {
            int iReturn = 1;
            string strOrderType = drSaleDsp.OrderType.Trim().ToUpper();
            if (strOrderType == "NT" || strOrderType == "CM" || strOrderType == "CC" || strOrderType == "NM" || strOrderType == "NC")
                iReturn = -1;

            decimal dReceiveSum =0;
            DataRow[] tblSelect = dsCentral.DebtAdjust_Date.Select(string.Format("OrderNbr = '{0}'", drSaleDsp.OrderNbr));
            for (int i = 0; i < tblSelect.Length; i++)
                if (!string.IsNullOrEmpty(tblSelect[i]["PayAmount"].ToString()))
            {
                dReceiveSum += Convert.ToDecimal(tblSelect[i]["PayAmount"]);
            }

            drARDoc.SaleOrderDspID = drSaleDsp.SlsOrdDspID;
            drARDoc.SalePersonID = drSaleDsp.SlsPerId;
            drARDoc.CustomerID = drSaleDsp.CustID;
            drARDoc.RoundID = drSaleDsp.RoundID;
            drARDoc.LineAmt = iReturn * (decimal)drSaleDsp.CuryLineAmt;
            drARDoc.LineFreeAmt = iReturn * CalculateLineFreeAmt(drSaleDsp.OrderNbr);
            drARDoc.LineDiscAmt = iReturn * (decimal)drSaleDsp.CuryLineDiscAmt;
            drARDoc.LineVolDiscAmt = iReturn * (decimal)drSaleDsp.CuryVolDiscAmt;
            drARDoc.CurryOrdAmt = iReturn * (decimal)drSaleDsp.CuryOrdAmt;
            drARDoc.OrdAmtTotal = (drARDoc.LineFreeAmt + drARDoc.LineDiscAmt + drARDoc.LineVolDiscAmt + drARDoc.CurryOrdAmt);
            drARDoc.ReceiveTotal = iReturn == 1 ? dReceiveSum : 0;
            drARDoc.DocBal = drARDoc.CurryOrdAmt;
            drARDoc.OrderDate = drSaleDsp.OrderDate;
            drARDoc.OrderType = drSaleDsp.OrderType;
            drARDoc.OrderNbr = drSaleDsp.OrderNbr;
            drARDoc.UpdateUser = string.Empty;
            drARDoc.UpdateDate = DateTime.Now;
        }

        private void AddNewARDocRow(DataCentralDataset.SlsOrdDspRow drSaleDsp)
        {
            DataCentralDataset.ARSaleDocRow drARDoc = dsCentral.ARSaleDoc.NewARSaleDocRow();
            AssignValueToARDoc(drSaleDsp, drARDoc);
            dsCentral.ARSaleDoc.AddARSaleDocRow(drARDoc);
        }

        private void ConvertDataToSystem(bool isUpdateSource)
        {
            SlsOrdDspTableAdapter daSlsOrderDsp = new SlsOrdDspTableAdapter();
            DspOrdDetTableAdapter daSlsOrderDetail = new DspOrdDetTableAdapter();
            InventoryTableAdapter daInventory = new InventoryTableAdapter();
            daInventory.Fill(dsCentral.Inventory);
            daSlsOrderDetail.Fill(dsCentral.DspOrdDet, dtpFromDate.DateTime.Date, dtpToDate.DateTime.Date);
            daSlsOrderDsp.Fill(dsCentral.SlsOrdDsp, dtpFromDate.DateTime.Date, dtpToDate.DateTime.Date);

            foreach (DataCentralDataset.SlsOrdDspRow drSaleDsp in dsCentral.SlsOrdDsp)
            {
                bool isExist = false;
                if (isUpdateSource)
                {
                    foreach (DataCentralDataset.ARSaleDocRow drARDoc in dsCentral.ARSaleDoc)
                    {
                        if (drARDoc.OrderNbr.Trim().Equals(drSaleDsp.OrderNbr.Trim()))
                        {
                            isExist = true;
                            AssignValueToARDoc(drSaleDsp, drARDoc);
                            break;
                        }
                    }
                    if (!isExist)
                        AddNewARDocRow(drSaleDsp);
                }
                else
                {
                    AddNewARDocRow(drSaleDsp);
                }
            }
        }

        private void ConvertSaleOrder_Load(object sender, EventArgs e)
        {
            dtpFromDate.DateTime = DateTime.Now;
            dtpToDate.DateTime = DateTime.Now;
            daARDoc.Fill(dsCentral.ARSaleDoc, dtpFromDate.DateTime.Date, dtpToDate.DateTime.Date);
            ValueListClass clValueList = new ValueListClass();
            iluCustomer.DataSource = clValueList.GetCustomerList();
            iluSalePerson.DataSource = clValueList.GetSaleSysPersonList();
            daDebtAdjust.Fill(dsCentral.DebtAdjust_Date, dtpFromDate.DateTime.Date, dtpToDate.DateTime.Date);
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            daARDoc.Fill(dsCentral.ARSaleDoc, dtpFromDate.DateTime.Date, dtpToDate.DateTime.Date);
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            ConvertDataToSystem(true);
            daARDoc.Update(dsCentral.ARSaleDoc);
        }
    }
}
